package com.w.m.start;

import com.w.m.config.AppConfig;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * tomcat 一启动就会加载这个类
 * 1）创建容器，指定配置类
 * 2）注册一个	DispatcherServlet
 * 3)以后所有的请求都会被	DispatcherServlet处理
 *
 * Servlet 是javaEE的标准，tomcat是Servlet的一个实现
 * Servlet 3.0  tomcat 6.0
 */
public class AppStart implements WebApplicationInitializer {
	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		//自己创建一个IOC容器
		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
		context.register(AppConfig.class);//注册配置类

		//自己new一个DispatcherServlet对象并传入一个容器
		DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
		ServletRegistration.Dynamic dynamic = servletContext.addServlet("app", dispatcherServlet);//利用servlet规范添加servlet
		dynamic.setLoadOnStartup(1);
		dynamic.addMapping("/");//添加映射
	}
}
